<?php



// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.view');

class SimplelistingViewFields extends JView

{
	var $pagination = null ;

    function display( $tpl = null )
	{
		switch ($tpl) {
			case 'form':
                        $this->editItem();
                        break;
			default :
                        $this->listIems();
		}

		parent::display($tpl);
	}


    function listIems()
	{

//       global $mainframe, $context;
        $model =& $this->getModel('fields', 'SimplelistingModel');
        $document = & JFactory::getDocument();
        $db  =& JFactory::getDBO();
        $uri =& JFactory::getURI();
        $user =&JFactory::getUser();
        $document->setTitle( JText::_('TITLE_MAIN') );
        JToolBarHelper::title(JText::_('TITLE_MAIN'). ' [ ' . JText::_( 'TITLE_FIELDS' ) . ' ]' , 'simplelisting.png' );
        JToolBarHelper::back( 'Control Panel', 'index.php?option=com_simplelisting' );
        JToolBarHelper::publishList();
        JToolBarHelper::unpublishList();
        JToolBarHelper::addNew();
        JToolBarHelper::editList();
        JToolBarHelper::deleteList();

        $application= JFactory::getApplication();
        $option = JRequest::getCmd( 'option').'-fields';

        $filter_state     = $application->getUserStateFromRequest( $option.'filter_state', 'filter_state', '', 'word' );
        $filter_order     = $application->getUserStateFromRequest( $option.'filter_order', 'filter_order', 'f.ordering', 'cmd' );
        $filter_order_Dir = $application->getUserStateFromRequest( $option.'filter_order_Dir',  'filter_order_Dir', '', 'word' );
                $search           = $application->getUserStateFromRequest( $option.'search', 'search', '', 'string' );
        $search           = JString::strtolower( $search );


        $lists['state']     = JHTML::_('grid.state', $filter_state);
        $lists['order']     = $filter_order;
        $lists['order_Dir'] = $filter_order_Dir;
        $lists['search']    = $search;
//
//
        $lists['state']     = JHTML::_('grid.state', $filter_state);
        $lists['order']     = $filter_order;
        $lists['order_Dir'] = $filter_order_Dir;


        $items              = & $this->get( 'Data');
        $total              = & $this->get( 'Total');
        $pagination         = & $this->get( 'Pagination' );



                

            $tablesTranslations = array(
                'categories'    => JText::_('DD_ITEM_CATEGORIES'),
                'types'         => JText::_('DD_ITEM_TYPES'),
                'extras'        => JText::_('DD_ITEM_EXTRAS'),
                'layouts'       => JText::_('DD_ITEM_LAYOUTS'),
                'facilities'    => JText::_('DD_ITEM_FACILITIES'),
                'countries'     => JText::_('DD_ITEM_COUNTRIES')
	    );

        $options = array();

        foreach ( $tablesTranslations as $tableName => $translation ) {
        	$options[] = JHTML::_('select.option', $tableName, JText::_( $translation ) );
        }
         
        $selectTableList = JHTML::_( 'select.genericlist',
                                     $options,
                                     'table',
                                     'class="inputbox" size="1" onchange="document.adminForm.submit();"' ,
                                     'value',
                                     'text',
                                      $this->get('tableName')
                                    );

        $titles = array(
            'categories'    => JText::_('TITLE_MAIN'). ' [ ' . JText::_( 'DD_CATEGORIES_TITLE' ) . ' ]',
            'types'         => JText::_('TITLE_MAIN'). ' [ ' . JText::_( 'DD_TYPES_TITLE' ) . ' ]',
            'extras'        => JText::_('TITLE_MAIN'). ' [ ' . JText::_( 'DD_EXTRAS_TITLE' ) . ' ]',
            'layouts'       => JText::_('TITLE_MAIN'). ' [ ' . JText::_( 'DD_LAYOUTS_TITLE' ) . ' ]',
            'facilities'    => JText::_('TITLE_MAIN'). ' [ ' . JText::_( 'DD_FACILITIES_TITLE' ) . ' ]',
            'countries'     => JText::_('TITLE_MAIN'). ' [ ' . JText::_( 'DD_COUNTRIES_TITLE' ) . ' ]'
		);
			
        $this->assignRef('user',		$user);
        $this->assignRef('lists',		$lists);
        $this->assignRef('items',		$items);
        $this->assignRef('pagination',          $pagination);
        $this->assignRef('request_url',         $uri->toString());
        $this->assignRef('selectTableList' ,    $selectTableList );
			


	}

	function editItem()
	{

            JRequest::setVar( 'hidemainmenu', 1 );

            $row =& $this->get('row');

            $tablesTranslations = array(
            'categories'    => JText::_('DD_ITEM_CATEGORIES'),
            'types'         => JText::_('DD_ITEM_TYPES'),
            'extras'        => JText::_('DD_ITEM_EXTRAS'),
            'layouts'       => JText::_('DD_ITEM_LAYOUTS'),
            'facilities'    => JText::_('DD_ITEM_FACILITIES'),
            'countries'     => JText::_('DD_ITEM_COUNTRIES')
        );

            $table_name = JText::_( $tablesTranslations[$this->get('tableName')] ) ;
        
            if ( !$row->id ) {
	        $title = $table_name . ' [ ' . JText::_( 'TITLE_NEW' ) . ' ]' ;
	    } else {
                $title  = $table_name . ' [ ' . JText::_( 'TITLE_EDIT' ) . ' : ' .  $row->name . ' ]' ;
	    }
		JToolBarHelper::title( $title , 'fields.png' );
		JToolBarHelper::save() ;
		JToolBarHelper::apply() ;
		JToolBarHelper::cancel() ;
	  
		$this->assignRef('row' , $row );
		$this->assign('tableName' , $this->get('tableName') );
	}
	
   

}
